package com.ziytek.web.citizen.listener;

import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.event.CapacityEvent;
import com.ziytek.web.citizen.service.itf.LocalCapacityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author fenghx
 * @version v1.0
 * @className CapacityEventListener
 * @descripton 功能描述
 * @date 2023/4/11 14:58
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CapacityEventListener {

    private final LocalCapacityService localCapacityService;

    @Async("myAsyncExecutor")
    @EventListener
    @Order(ServerConstant.DATA_TYPE_LEN_1)
    public void capacityListener(CapacityEvent event) {
        if (event == null || StringUtils.isEmpty(event.getCabinetNo()) || StringUtils.isEmpty(event.getData())) {
            log.info("capacityListener 设备上送本地容量统计数据为空，不处理");
            return;
        }

        log.info("设备[{}]上送本地容量统计数据处理", event.getCabinetNo());
        localCapacityService.localCapacityProcess(event.getCabinetNo(), event.getData());
        log.info("设备[{}]上送本地容量统计数据处理完成", event.getCabinetNo());
    }
}
